package com.choosefine.base.shixiaobao.device.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.base.shixiaobao.base.BaseController;
import com.choosefine.base.shixiaobao.common.def.CommonConstant;
import com.choosefine.base.shixiaobao.common.def.ResultCode;
import com.choosefine.base.shixiaobao.device.model.DeviceInfo;
import com.choosefine.base.shixiaobao.device.service.DeviceInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author xiongchao 2017/7/19
 * @category 设备详情
 */
@RestController
@RequestMapping("/deviceinfo")
@Api(value = "设备详情", description = "熊朝")
public class DeviceInfoController extends BaseController {
	Logger logger = LoggerFactory.getLogger(DeviceInfoController.class);

	@Autowired
	DeviceInfoService deviceInfoService;

	@ApiOperation(value = "根据设备主键编号获取设备详情", notes = "传入设备主键编号，根据设备主键编号获取设备详情")
	@RequestMapping(value = "/id/{id}", method = RequestMethod.GET)
	public String getDeviceInfoById(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			DeviceInfo deviceInfo = deviceInfoService.getDeviceInfoById(id);
			result = responseSuccess(deviceInfo);
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "保存设备详情", notes = "保存设备详情")
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST)
	public String saveDeviceInfo(@Valid @RequestBody DeviceInfo deviceInfo) throws Exception {
		String result = null;
		if (deviceInfo != null) {
			deviceInfo.setCtime(System.currentTimeMillis());
			deviceInfo.setIsValid(CommonConstant.VALID);
			Integer id = deviceInfoService.save(deviceInfo);
			if (id != null)
				result = responseSuccess(id);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_save_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "更新设备详情", notes = "更新设备详情")
	@RequestMapping(method = RequestMethod.PUT)
	public String updateDeviceInfo(@RequestBody DeviceInfo deviceInfo) throws Exception {
		String result = null;
		if (deviceInfo != null) {
			deviceInfo.setUtime(System.currentTimeMillis());
			Integer num = deviceInfoService.update(deviceInfo);
			if (num != null)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_update_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}

	@ApiOperation(value = "删除设备详情", notes = "删除设备详情")
	@RequestMapping(value = "id/{id}", method = RequestMethod.DELETE)
	public String deleteDeviceInfo(@PathVariable Integer id) throws Exception {
		String result = null;
		if (id != null) {
			Integer num = deviceInfoService.delete(id);
			if (num != null && num > 0)
				result = responseSuccess(num);
			else
				result = responseFail(ResultCode.MSG_ERROR,
						messageSourceUtils.getMessage("attendance.exception.msg_delete_fail"));
		} else
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		return result;
	}
}
